//
//  BActivateAdministratorVC.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/12/9.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BActivateAdministratorVC.h"
#import "KaiJuFaPiaoViewController.h" //开具发票
#import <WXApi.h>
#import <AlipaySDK/AlipaySDK.h>
#import "APAuthInfo.h"
#import "APRSASigner.h"
#import "DISBMProfitViewController.h"
#import "CSMyInfoViewController.h"

@interface BActivateAdministratorVC () <WXApiDelegate>
@property (nonatomic, strong) FaPiaoInfoParamsModel *mymodel;
@property (nonatomic, strong) UIButton *alipayBtn; //支付宝
@property (nonatomic, strong) UIButton *wechatBtn; //微信
@property (nonatomic, strong) UILabel *fpConLab; //开具发票Lab
@property (nonatomic, strong) NSString *btnTag;
@property (nonatomic, strong) NSDictionary *dataDic;
@property (nonatomic, strong) UILabel *youxiaoqiConLab;
@property (nonatomic, strong) UILabel *amountConLab;

@end

@implementation BActivateAdministratorVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self preferredStatusBarStyle];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    // 返回你所需要的状态栏样式
    return UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"激活管理员身份";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    self.dataDic = [NSDictionary dictionary];
    
    [Center addObserver:self selector:@selector(paySucceedHandle) name:@"PaySuccess" object:nil];
    [self BActivateAdministratorUI];
    self.mymodel = [[FaPiaoInfoParamsModel alloc]init];
    self.mymodel.invoice  = @"0";
    self.btnTag = @"1";
    
    [self request];
}
//支付成功之后的回调处理
-(void)paySucceedHandle
{
    //判断是从哪个vc push过来的
      NSArray *vcsArray = self.navigationController.viewControllers;
      NSInteger vcCount = vcsArray.count;
      UIViewController *lastVC = vcsArray[vcCount-2];
    //我的收益
      if ([lastVC isKindOfClass:[DISBMProfitViewController class]]) {
          [self.navigationController popToViewController:lastVC animated:YES];
      }
    //个人信息
      else if ([lastVC isKindOfClass:[CSMyInfoViewController class]]) {
         [self.navigationController popToViewController:lastVC animated:YES];
      }
}
- (void)request {
    
    [NetWorkTool POST:actionPower param:nil success:^(id dic) {
        self.dataDic = dic[@"data"];
        NSString *data = [self.dataDic objectForKey:@"date"];
        NSString *subStr = [data substringWithRange:NSMakeRange(0, 10)];
        self.youxiaoqiConLab.text = subStr;
        self.amountConLab.text = [NSString stringWithFormat:@"%@%@", self.dataDic[@"money"], @"元"];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

- (void)BActivateAdministratorUI {
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, 174)];
    topView.backgroundColor = [UIColor colorWithHexString:@"#353638"];
    [self.view addSubview:topView];
    
    UILabel *adminLab = [[UILabel alloc] initWithFrame:CGRectMake(16, 28, KWIDTH-32, 37)];
    adminLab.text = @"尊敬的管理员";
    adminLab.textColor = [UIColor colorWithHexString:@"#E3B07C"];
    adminLab.textAlignment = NSTextAlignmentCenter;
    adminLab.font = KFontPingFangSCMedium(26);
    [topView addSubview:adminLab];
    
    UILabel *jihuoxuzhiLab = [[UILabel alloc] initWithFrame:CGRectMake(16, adminLab.bottom + 14, 82, 22)];
    jihuoxuzhiLab.text = @"激活须知:";
    jihuoxuzhiLab.textColor = [UIColor colorWithHexString:@"#E3B07C"];
    jihuoxuzhiLab.textAlignment = NSTextAlignmentLeft;
    jihuoxuzhiLab.font = KFontPingFangSCMedium(16);
    [topView addSubview:jihuoxuzhiLab];
    
    UILabel *youxiaoqiTitLab = [[UILabel alloc] initWithFrame:CGRectMake(16, jihuoxuzhiLab.bottom + 6, 90, 22)];
    youxiaoqiTitLab.text = @"1.有效期至：";
    youxiaoqiTitLab.textColor = [UIColor colorWithHexString:@"#E3B07C"];
    youxiaoqiTitLab.textAlignment = NSTextAlignmentLeft;
    youxiaoqiTitLab.font = KFontPingFangSCMedium(14);
    [topView addSubview:youxiaoqiTitLab];
    
    UILabel *youxiaoqiConLab = [[UILabel alloc] initWithFrame:CGRectMake(youxiaoqiTitLab.right, jihuoxuzhiLab.bottom + 6, 120, 22)];
//    NSString *data = [self.dataDic objectForKey:@"date"];
//    NSString *subStr = [data substringWithRange:NSMakeRange(0, 10)];
//    youxiaoqiConLab.text = subStr;
    youxiaoqiConLab.textColor = [UIColor colorWithHexString:@"#E3B07C"];
    youxiaoqiConLab.textAlignment = NSTextAlignmentLeft;
    youxiaoqiConLab.font = KFontPingFangSCMedium(14);
    [topView addSubview:youxiaoqiConLab];
    self.youxiaoqiConLab = youxiaoqiConLab;
    
    UILabel *tipsTitLab = [[UILabel alloc] initWithFrame:CGRectMake(16, youxiaoqiTitLab.bottom + 6, KWIDTH -32, 22)];
    tipsTitLab.text = @"2.管理员会费一经缴纳不予退还。";
    tipsTitLab.textColor = [UIColor colorWithHexString:@"#E3B07C"];
    tipsTitLab.textAlignment = NSTextAlignmentLeft;
    tipsTitLab.font = KFontPingFangSCMedium(14);
    [topView addSubview:tipsTitLab];
    
    //会费金额 View
    UIView *amountOfDuesV = [[UIView alloc] initWithFrame:CGRectMake(0, topView.bottom +10, KWIDTH, 44)];
    amountOfDuesV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:amountOfDuesV];
    
    //会费金额 标题
    UILabel *amountTitLab = [[UILabel alloc] initWithFrame:CGRectMake(16, 11, 90, 22)];
    amountTitLab.text = @"会费金额";
    amountTitLab.textColor = [UIColor colorWithHexString:@"#232620"];
    amountTitLab.textAlignment = NSTextAlignmentLeft;
    amountTitLab.font = KFontPingFangSCMedium(16);
    [amountOfDuesV addSubview:amountTitLab];
    
    //会费金额 内容
    UILabel *amountConLab = [[UILabel alloc] initWithFrame:CGRectMake(amountOfDuesV.right -90-16, 11, 90, 22)];
    //amountConLab.text = [NSString stringWithFormat:@"%@%@", self.dataDic[@"money"], @"元"];
    amountConLab.textColor = [UIColor colorWithHexString:@"#232620"];
    amountConLab.textAlignment = NSTextAlignmentRight;
    amountConLab.font = KFontPingFangSCMedium(16);
    [amountOfDuesV addSubview:amountConLab];
    self.amountConLab = amountConLab;
    
    //选择支付方式 标题
    UILabel *selectTitLab = [[UILabel alloc] initWithFrame:CGRectMake(16, amountOfDuesV.bottom +16, 120, 22)];
    selectTitLab.text = @"选择支付方式";
    selectTitLab.textColor = [UIColor colorWithHexString:@"#666666"];
    selectTitLab.textAlignment = NSTextAlignmentLeft;
    selectTitLab.font = KFontPingFangSCMedium(16);
    [self.view addSubview:selectTitLab];
    
    //支付 View
    UIView *centerPayV = [[UIView alloc] initWithFrame:CGRectMake(0, selectTitLab.bottom +10, KWIDTH, 112)];
    centerPayV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:centerPayV];
    
    //支付宝支付 Logo
    UIImageView *zhifubaoImg = [[UIImageView alloc] initWithFrame:CGRectMake(16, 12, 32, 32)];
    [zhifubaoImg setImage:imgname(@"zhifubao")];
    [centerPayV addSubview:zhifubaoImg];
    
    //支付宝支付 标题
    UILabel *zfbTitLab = [[UILabel alloc] initWithFrame:CGRectMake(zhifubaoImg.right +16, 10, 72, 20)];
    zfbTitLab.text = @"支付宝";
    zfbTitLab.textColor = [UIColor colorWithHexString:@"#333333"];
    zfbTitLab.textAlignment = NSTextAlignmentLeft;
    zfbTitLab.font = KFontPingFangSCMedium(14);
    [centerPayV addSubview:zfbTitLab];
    
    //支付宝支付 描述文本
    UILabel *zfbConLab = [[UILabel alloc] initWithFrame:CGRectMake(zhifubaoImg.right +16, zfbTitLab.bottom +6, KWIDTH /2, 14)];
    zfbConLab.text = @"支付宝支付余额和银行充值";
    zfbConLab.textColor = [UIColor colorWithHexString:@"#666666"];
    zfbConLab.textAlignment = NSTextAlignmentLeft;
    zfbConLab.font = KFontPingFangSCMedium(10);
    [centerPayV addSubview:zfbConLab];
    
    //支付宝选择按钮
    UIButton *alipayBtn = [[UIButton alloc] initWithFrame:CGRectMake(centerPayV.right - 40, 12, 30, 30)];
    [alipayBtn addTarget:self action:@selector(alipayBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [alipayBtn setImage:[UIImage imageNamed:@"weixuanzhong"] forState:(UIControlStateNormal)];
    [alipayBtn setImage:[UIImage imageNamed:@"xuanzhong"] forState:(UIControlStateSelected)];
    [centerPayV addSubview:alipayBtn];
    self.alipayBtn = alipayBtn;
    self.alipayBtn.tag = 1;
    
    //分割线
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(16, zfbConLab.bottom +6, KWIDTH-16, 1)];
    line.backgroundColor = [UIColor colorWithHexString:@"#DDDBD7"];
    [centerPayV addSubview:line];
    
    //微信支付 图片
    UIImageView *wechatImg = [[UIImageView alloc] initWithFrame:CGRectMake(16, line.bottom +13, 32, 32)];
    [wechatImg setImage:imgname(@"wechatpay")];
    [centerPayV addSubview:wechatImg];
    
    //微信支付 标题
    UILabel *wxTitLab = [[UILabel alloc] initWithFrame:CGRectMake(zhifubaoImg.right +16, line.bottom +10, 72, 20)];
    wxTitLab.text = @"微信";
    wxTitLab.textColor = [UIColor colorWithHexString:@"#333333"];
    wxTitLab.textAlignment = NSTextAlignmentLeft;
    wxTitLab.font = KFontPingFangSCMedium(14);
    [centerPayV addSubview:wxTitLab];
    
    //微信支付 描述文本
    UILabel *wxConLab = [[UILabel alloc] initWithFrame:CGRectMake(zhifubaoImg.right +16, wxTitLab.bottom +6, KWIDTH /2, 14)];
    wxConLab.text = @"亿万用户的选择,更快更安全";
    wxConLab.textColor = [UIColor colorWithHexString:@"#666666"];
    wxConLab.textAlignment = NSTextAlignmentLeft;
    wxConLab.font = KFontPingFangSCMedium(10);
    [centerPayV addSubview:wxConLab];
    
    //微信选择按钮
    UIButton *wechatBtn = [[UIButton alloc] initWithFrame:CGRectMake(centerPayV.right - 40, line.bottom +12, 30, 30)];
    [wechatBtn addTarget:self action:@selector(wechatBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [wechatBtn setImage:[UIImage imageNamed:@"weixuanzhong"] forState:(UIControlStateNormal)];
    [wechatBtn setImage:[UIImage imageNamed:@"xuanzhong"] forState:(UIControlStateSelected)];
    [centerPayV addSubview:wechatBtn];
    self.wechatBtn = wechatBtn;
    self.wechatBtn.tag = 2;
    
    //开具发票 View
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, centerPayV.bottom +16, KWIDTH, 44)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(event:)];
    [bottomView addGestureRecognizer:tapGesture];
    [tapGesture setNumberOfTapsRequired:1];
    
    //开具发票 标题
    UILabel *fpTitLab = [[UILabel alloc] initWithFrame:CGRectMake(16, 11, 90, 22)];
    fpTitLab.text = @"开具发票";
    fpTitLab.textColor = [UIColor colorWithHexString:@"#232620"];
    fpTitLab.textAlignment = NSTextAlignmentLeft;
    fpTitLab.font = KFontPingFangSCMedium(16);
    [bottomView addSubview:fpTitLab];
    
    //开具发票 内容
    UILabel *fpConLab = [[UILabel alloc] initWithFrame:CGRectMake(amountOfDuesV.right -90-35, 11, 90, 22)];
    fpConLab.text = @"不开发票";
    fpConLab.textColor = [UIColor colorWithHexString:@"#666666"];
    fpConLab.textAlignment = NSTextAlignmentRight;
    fpConLab.font = KFontPingFangSCMedium(14);
    [bottomView addSubview:fpConLab];
    self.fpConLab = fpConLab;
    
    //开具发票 箭头
    UIImageView *rightImg = [[UIImageView alloc] initWithFrame:CGRectMake(bottomView.right-35, 9.5, 25, 25)];
    [rightImg setImage:imgname(@"chevron")];
    [bottomView addSubview:rightImg];
    
    //确认支付按钮
    UIButton *surePayBtn = [[UIButton alloc] initWithFrame:CGRectMake(27, bottomView.bottom +46, KWIDTH - 27*2, 48)];
    surePayBtn.backgroundColor = [UIColor colorWithHexString:@"#C6A47E"];
    surePayBtn.layer.cornerRadius = 4;
    [surePayBtn setTitle:@"确认支付" forState:UIControlStateNormal];
    surePayBtn.titleLabel.font = KFontPingFangSCMedium(16);
    [surePayBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];;
    [surePayBtn addTarget:self action:@selector(sureBtnAciontClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:surePayBtn];
    
    // 先默认一个选项
    _alipayBtn.selected = YES;
}

//支付宝按钮点击事件处理
- (void)alipayBtnAction:(UIButton *)button {
    if (!_alipayBtn.selected){
        _wechatBtn.selected = NO;
        [_alipayBtn setImage:[UIImage imageNamed:@"xuanzhong"] forState:(UIControlStateNormal)];
        [_wechatBtn setImage:[UIImage imageNamed:@"weixuanzhong"] forState:(UIControlStateNormal)];
        self.btnTag = @"1";
    } else if (_alipayBtn.selected) {
        _wechatBtn.selected = NO;
    }
}

//微信按钮点击事件处理
- (void)wechatBtnAction:(UIButton *)button {
    if (!_wechatBtn.selected) {
        _alipayBtn.selected = NO;
        [_wechatBtn setImage:[UIImage imageNamed:@"xuanzhong"] forState:(UIControlStateNormal)];
        [_alipayBtn setImage:[UIImage imageNamed:@"weixuanzhong"] forState:(UIControlStateNormal)];
        self.btnTag = @"2";
    } else if (_wechatBtn.selected) {
        _alipayBtn.selected = NO;
    }
}

//执行触发的方法
- (void)event:(UITapGestureRecognizer *)gesture {
    KaiJuFaPiaoViewController *vc = [[KaiJuFaPiaoViewController alloc]init];
    vc.faPiaoInfoHandle = ^(FaPiaoInfoParamsModel * _Nonnull model) {
        if ([model.invoice integerValue] == 0) {
            self.fpConLab.text = @"不开发票";
        } else {
            self.fpConLab.text = @"开具发票";
        }
        self.mymodel = model;
    };
    [self.navigationController pushViewController:vc animated:YES];
}

//确认支付按钮 点击事件处理
- (void)sureBtnAciontClick:(UIButton *)button {
    [self requestData];
    KMyLog(@"你点击了 激活管理员身份 界面的 确认支付按钮");
}

//确认支付 数据请求
- (void)requestData {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"type"] = self.btnTag; //1支付宝2微信
    param[@"invoice"] = self.mymodel.invoice; //是否开发票0不开1开
    param[@"invoiceType"] = self.mymodel.invoiceType; //发票类型1个人2公司
    param[@"title"] = self.mymodel.title; //抬头
    param[@"phone"] = self.mymodel.phone; //电话
    param[@"email"] = self.mymodel.email; //邮箱地址
    
    [NetWorkTool POST:hasPower param:param success:^(id dic) {
        KMyLog(@"_____________ %@", dic);
        //---------调取支付---------//
        if ([[dic objectForKey:@"code"] integerValue] == 0) {
            if ([self.btnTag isEqualToString:@"1"]) {
                //默认支付宝
                NSString *appScheme = @"jingxiaoshangzsyh";
                // NOTE: 将签名成功字符串格式化为订单字符串,请严格按照该格式
                // NSString *orderString = [NSString stringWithFormat:@"%@&sign=%@",
                //                           orderInfoEncoded, signedString];
                NSString *orderString =[NSString stringWithFormat:@"%@",[dic objectForKey:@"data"]];
                // NOTE: 调用支付结果开始支付
                [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                    
                    NSLog(@"支付宝reslut = %@",resultDic);
                    if ([[resultDic objectForKey:@"resultStatus"] integerValue] == 9000) {
                        [self showOnleText:[dic objectForKey:@"memo"] delay:1.5];
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            
                        });
                    }else{
                        [self showOnleText:[dic objectForKey:@"memo"] delay:1.5];
                    }
                }];
            }else   if ([self.btnTag isEqualToString:@"2"]){
                NSMutableDictionary *diccc = [NSMutableDictionary dictionaryWithDictionary:[dic objectForKey:@"data"]];
                
                //微信
                //需要创建这个支付对象
                PayReq *req   = [[PayReq alloc] init];
                //由用户微信号和AppID组成的唯一标识，用于校验微信用户
                req.openID = [NSString stringWithFormat:@"%@",[diccc objectForKey:@"appid"]];
                // 商家id，在注册的时候给的
                req.partnerId = [NSString stringWithFormat:@"%@",[diccc objectForKey:@"partnerid"]];;
                // 预支付订单这个是后台跟微信服务器交互后，微信服务器传给你们服务器的，你们服务器再传给你
                req.prepayId  = [NSString stringWithFormat:@"%@",[diccc objectForKey:@"prepayid"]];;
                // 根据财付通文档填写的数据和签名
                req.package  = [NSString stringWithFormat:@"%@",[diccc objectForKey:@"package"]];;
                // 随机编码，为了防止重复的，在后台生成
                req.nonceStr  = [NSString stringWithFormat:@"%@",[diccc objectForKey:@"noncestr"]];;
                // 这个是时间戳，也是在后台生成的，为了验证支付的
                NSString * stamp = [NSString stringWithFormat:@"%@",[diccc objectForKey:@"timestamp"]];
                req.timeStamp = stamp.intValue;
                // 这个签名也是后台做的
                req.sign = [diccc objectForKey:@"sign"];
                //发送请求到微信，等待微信返回onResp
                [WXApi sendReq:req];
            }
        }
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

@end

